#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"
long long int String_To_Int(char *string, int Length)
{

    long long int  Temp_Value = 0;
    int i = 0;
    long long int Times_16 = 1;

    //Caculate the length of the string
    printf("The Length is %d\r\n",Length);
    
    //Convert every character to a value
    for( i = Length - 1 ; i >= 0;i--)
    {
       if((string[i] >= 0x30) && (string[i] <= 0x39))
       {
           Temp_Value += ((string[i] - 0x30)*Times_16);
           Times_16 *= 16;
         } else if((string[i] >= 0x41) &&( string[i] <= 0x46)) { Temp_Value += ((string[i] - (0x41 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else if((string[i] >= 0x61) &&( string[i] <= 0x66))
       {
           Temp_Value += ((string[i] - (0x61 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else
       { 
           printf("Wrong number!\r\n");
           return -1;
       }
       printf(" string[i] is %c and Temp_Value is %llx \r\n",string[i],Temp_Value);
    } 
    printf("Temp_Value is %llx\r\n",Temp_Value);

    return Temp_Value;
}
long long int Value_Complement(long long int value,long long int Length) 
{
    long long int Temp_Value;
    Length *= 4;
    if(Length >= 32)
    {
        Temp_Value = (1 << 30);
        Temp_Value = Temp_Value * ( 1  << (Length - 30));
        Temp_Value = Temp_Value - 1 - 0xff;
    }
    else 
        Temp_Value = ((1 << Length) - 1 - 0xff);
    printf("%s:\r\n Temp_Value is %llx\r\n Length is %lld\r\n Temp_Value is %llx\r\n",__func__,Temp_Value,Length,Temp_Value);
    value = (Temp_Value - (value & Temp_Value)) | (value & 0xff);
    printf("Surppose to be %llx\r\n",value);

    return value;
   
}
int main(int argc, char *argv[])
{
    int *x = 0;
    long long int Temp_Value = 0;
    long long int value = 0;
    long long int Length = 0;
    if(argc > 1)
    {
        printf(" argv[1] = %s\r\n",argv[1]);
        Length = strlen(argv[1]);  
        Temp_Value = String_To_Int(argv[1],Length);
        if((Temp_Value != -1) && (Temp_Value != 0))
        {
            value = Value_Complement(Temp_Value,Length); 
            printf("Your value should be 0x%llx\r\n",value); }
    }
    else
    {
        printf("Need more than one argv.\r\n");
        return 0;
    }

        return 0;
} 
